clear
use "$path\Intermediary Data\DataReg_RotterdamBrent"
merge 1:1 date_tokeep using "$path\Intermediary Data\DataReg_ResidMA_Rotterdam_daily.dta"
drop _merge
merge 1:1 date_tokeep using "$path\Intermediary Data\Brent_daily.dta"
drop _merge

keep if date_maj_num!=. 

duplicates drop date_maj_num, force

keep date_maj_num diesel_rotterdam_euro resid_movav_diesel  brent eurusd
 
tsset date_maj_num

gen d_dieselr=(ln(diesel_rotterdam_euro)-ln(diesel_rotterdam_euro[_n-1]))*100
gen brenteuro=brent/eurusd
gen d_brentr=(ln(brenteuro)-ln(brenteuro[_n-1]))*100
replace resid_movav_diesel=resid_movav_diesel*100

keep date_maj_num d_dieselr resid_movav_diesel d_brentr diesel_rotterdam_euro brenteuro

format date_maj_num %d
gen date_year = yofd(date_maj_num)

drop if d_brentr ==.

gen l_d_dieselr=d_dieselr[_n-1]
gen l2_d_dieselr=d_dieselr[_n-2]
gen l3_d_dieselr=d_dieselr[_n-3]
gen l4_d_dieselr=d_dieselr[_n-4]
gen l5_d_dieselr=d_dieselr[_n-5]


gen l_d_brentr=d_brentr[_n-1]
gen l2_d_brentr=d_brentr[_n-2]
gen l3_d_brentr=d_brentr[_n-3]
gen l4_d_brentr=d_brentr[_n-4]
gen l5_d_brentr=d_brentr[_n-5]

gen l_resid_movav_diesel=resid_movav_diesel[_n-1]
gen l2_resid_movav_diesel=resid_movav_diesel[_n-2]
gen l3_resid_movav_diesel=resid_movav_diesel[_n-3]
gen l4_resid_movav_diesel=resid_movav_diesel[_n-4]
gen l5_resid_movav_diesel=resid_movav_diesel[_n-5]


*** Rotterdam baseline
graph twoway (tsline d_dieselr, tlabel(01jan2007 "01/2007" 01jan2008 "01/2008" 01jan2009 "01/2009" 01jan2010 "01/2010" 01jan2011 "01/2011" 01jan2012 "01/2012" ///
01jan2013 "01/2013" 01jan2014 "01/2014" 01jan2015 "01/2015" 01jan2016 "01/2016" 01jan2017 "01/2017" 01jan2018 "01/2018", alternate) ///
graphregion(color(white)) ytitle("Daily variation (%)") xtitle("") ylabel(-20(5)20))

graph export "$path\Graphs_Tables\FigureA3_a.png", replace
graph export "$path\Graphs_Tables\FigureA3_a.eps", replace


*** Brent
graph twoway (tsline d_brentr, tlabel(01jan2007 "01/2007" 01jan2008 "01/2008" 01jan2009 "01/2009" 01jan2010 "01/2010" 01jan2011 "01/2011" 01jan2012 "01/2012" ///
01jan2013 "01/2013" 01jan2014 "01/2014" 01jan2015 "01/2015" 01jan2016 "01/2016" 01jan2017 "01/2017" 01jan2018 "01/2018", alternate) ///
graphregion(color(white)) ytitle("Daily variation (%)") xtitle("") ylabel(-20(5)20) color(sienna))

graph export "$path\Graphs_Tables\FigureA3_b.png", replace
graph export "$path\Graphs_Tables\FigureA3_b.eps", replace

*** MA
graph twoway (tsline resid_movav_diesel, tlabel(01jan2007 "01/2007" 01jan2008 "01/2008" 01jan2009 "01/2009" 01jan2010 "01/2010" 01jan2011 "01/2011" 01jan2012 "01/2012" ///
01jan2013 "01/2013" 01jan2014 "01/2014" 01jan2015 "01/2015" 01jan2016 "01/2016" 01jan2017 "01/2017" 01jan2018 "01/2018", alternate) ///
graphregion(color(white)) ytitle("Daily variation (%)") xtitle("") ylabel(-20(5)20) color(gs5))

graph export "$path\Graphs_Tables\FigureA3_c.png", replace
graph export "$path\Graphs_Tables\FigureA3_c.eps", replace



**** Table A2

preserve

gen average=.
gen p25=.
gen p50=.
gen p75=.
gen sd=.
gen skew=.
gen kurtosis=.


summ d_dieselr, detail
replace average=r(mean) if _n==1
replace p25=r(p25) if _n==1
replace p50=r(p50) if _n==1
replace p75=r(p75) if _n==1
replace sd=r(sd) if _n==1
replace skew=r(skewness) if _n==1
replace kurtosis=r(kurtosis) if _n==1

summ resid_movav_diesel, detail 
replace average=r(mean) if _n==2
replace p25=r(p25) if _n==2
replace p50=r(p50) if _n==2
replace p75=r(p75) if _n==2
replace sd=r(sd) if _n==2
replace skew=r(skewness) if _n==2
replace kurtosis=r(kurtosis) if _n==2

summ d_brentr, detail 
replace average=r(mean) if _n==3
replace p25=r(p25) if _n==3
replace p50=r(p50) if _n==3
replace p75=r(p75) if _n==3
replace sd=r(sd) if _n==3
replace skew=r(skewness) if _n==3
replace kurtosis=r(kurtosis) if _n==3

gen Var="Rotterdam price change" if _n==1
replace Var ="Gap between Rotterdam price and its moving average" if _n==2
replace Var ="Brent price changes" if _n==3

order  Var average p25 p50 p75 sd skew kurtosis
replace average=round(average, 0.01)
replace p25=round(p25, 0.01)
replace p50=round(p50, 0.01)
replace p75=round(p75, 0.01)
replace sd=round(sd, 0.01)
replace skew=round(skew, 0.01)
replace kurtosis=round(kurtosis, 0.01)
format %9.2f  average p25 p50 p75 sd skew kurtosis
keep Var average p25 p50 p75 sd skew kurtosis
keep if _n<=3
save "$path\Graphs_Tables\TableA2_edited.dta", replace

restore


*** Table A3
preserve

gen level=.
gen logchange=.

dfuller diesel_rotterdam_euro, regress 
replace level=r(Zt) if _n==1
dfuller d_dieselr, regress 
replace logchange=r(Zt) if _n==1
dfuller brenteuro, regress 
replace level=r(Zt) if _n==2
dfuller d_brentr, regress 
replace logchange=r(Zt) if _n==2
dfuller resid_movav_diesel, regress 
replace level=r(Zt) if _n==3

gen Var="Rotterdam price change" if _n==1
replace Var ="Brent price changes" if _n==2
replace Var ="Gap between Rotterdam price and its moving average" if _n==3

order  Var level logchange
replace level=round(level, 0.001)
replace logchange=round(logchange, 0.001)
format %9.3f level logchange
keep Var level logchange
keep if _n<=3

save "$path\Graphs_Tables\TableA3_edited.dta", replace

restore


*** Table A4

gen rotterdam_price_changes=.
gen brent_price_changes=.
gen gap_rotterdam_price_ma=.

reg d_dieselr l_d_dieselr l2_d_dieselr l3_d_dieselr l4_d_dieselr l5_d_dieselr, robust
replace rotterdam_price_changes=_b[l_d_dieselr] if _n==1
replace rotterdam_price_changes=_se[l_d_dieselr] if _n==2
replace rotterdam_price_changes=_b[l2_d_dieselr] if _n==3
replace rotterdam_price_changes=_se[l2_d_dieselr] if _n==4
replace rotterdam_price_changes=_b[l3_d_dieselr] if _n==5
replace rotterdam_price_changes=_se[l3_d_dieselr] if _n==6
replace rotterdam_price_changes=_b[l4_d_dieselr] if _n==7
replace rotterdam_price_changes=_se[l4_d_dieselr] if _n==8
replace rotterdam_price_changes=_b[l5_d_dieselr] if _n==9
replace rotterdam_price_changes=_se[l5_d_dieselr] if _n==10
replace rotterdam_price_changes=_b[_cons] if _n==11
replace rotterdam_price_changes=_se[_cons] if _n==12
replace rotterdam_price_changes=e(r2) if _n==13
replace rotterdam_price_changes=e(N) if _n==14

reg d_brentr l_d_brentr l2_d_brentr l3_d_brentr l4_d_brentr l5_d_brentr, robust
replace brent_price_changes=_b[l_d_brentr] if _n==1
replace brent_price_changes=_se[l_d_brentr] if _n==2
replace brent_price_changes=_b[l2_d_brentr] if _n==3
replace brent_price_changes=_se[l2_d_brentr] if _n==4
replace brent_price_changes=_b[l3_d_brentr] if _n==5
replace brent_price_changes=_se[l3_d_brentr] if _n==6
replace brent_price_changes=_b[l4_d_brentr] if _n==7
replace brent_price_changes=_se[l4_d_brentr] if _n==8
replace brent_price_changes=_b[l5_d_brentr] if _n==9
replace brent_price_changes=_se[l5_d_brentr] if _n==10
replace brent_price_changes=_b[_cons] if _n==11
replace brent_price_changes=_se[_cons] if _n==12
replace brent_price_changes=e(r2) if _n==13
replace brent_price_changes=e(N) if _n==14

reg resid_movav_diesel l_resid_movav_diesel l2_resid_movav_diesel l3_resid_movav_diesel l4_resid_movav_diesel l5_resid_movav_diesel, robust 
replace gap_rotterdam_price_ma=_b[l_resid_movav_diesel] if _n==1
replace gap_rotterdam_price_ma=_se[l_resid_movav_diesel] if _n==2
replace gap_rotterdam_price_ma=_b[l2_resid_movav_diesel] if _n==3
replace gap_rotterdam_price_ma=_se[l2_resid_movav_diesel] if _n==4
replace gap_rotterdam_price_ma=_b[l3_resid_movav_diesel] if _n==5
replace gap_rotterdam_price_ma=_se[l3_resid_movav_diesel] if _n==6
replace gap_rotterdam_price_ma=_b[l4_resid_movav_diesel] if _n==7
replace gap_rotterdam_price_ma=_se[l4_resid_movav_diesel] if _n==8
replace gap_rotterdam_price_ma=_b[l5_resid_movav_diesel] if _n==9
replace gap_rotterdam_price_ma=_se[l5_resid_movav_diesel] if _n==10
replace gap_rotterdam_price_ma=_b[_cons] if _n==11
replace gap_rotterdam_price_ma=_se[_cons] if _n==12
replace gap_rotterdam_price_ma=e(r2) if _n==13
replace gap_rotterdam_price_ma=e(N) if _n==14

gen Var="1-day lag" if _n==1
replace Var="2-day lag" if _n==3
replace Var="3-day lag" if _n==5
replace Var="4-day lag" if _n==7
replace Var="5-day lag" if _n==9
replace Var="Intercept" if _n==11
replace Var="R2" if _n==13
replace Var="Nobs" if _n==14

keep Var rotterdam_price_changes brent_price_changes gap_rotterdam_price_ma
rename rotterdam_price_changes col1
rename brent_price_changes col2
rename gap_rotterdam_price_ma col3

gen tstatcol1=.
gen tstatcol2=.
gen tstatcol3=.



foreach i in 1 3 5 7 9 11 {
replace tstatcol1=col1[`i']/col1[`i'+1] if _n==`i'
replace tstatcol2=col2[`i']/col2[`i'+1] if _n==`i'
replace tstatcol3=col3[`i']/col3[`i'+1] if _n==`i'
}

gen starscol1=""
gen starscol2=""
gen starscol3=""


foreach i in 1 2 3 {
replace starscol`i'="***" if abs(tstatcol`i')>=2.58 & tstatcol`i'!=.
replace starscol`i'="**" if abs(tstatcol`i')<2.58 & abs(tstatcol`i')>=1.96 & tstatcol`i'!=.
replace starscol`i'="*" if abs(tstatcol`i')<1.96 & abs(tstatcol`i')>=1.64 & tstatcol`i'!=.
}

drop tstat*
rename col* coltemp*

foreach i in 1 2 3 {
replace coltemp`i'=round(coltemp`i', 0.001)
tostring coltemp`i', gen(coltempbis`i') force format("%9.3f")  
egen col`i'=concat(coltempbis`i' starscol`i')
}

rename col1 rotterdam_price_changes 
rename col2 brent_price_changes 
rename col3 gap_rotterdam_price_ma 

keep Var rotterdam_price_changes brent_price_changes gap_rotterdam_price_ma

order  Var rotterdam_price_changes brent_price_changes gap_rotterdam_price_ma


keep if _n<=14

save "$path\Graphs_Tables\TableA4_edited.dta", replace
